package es.upv.epsa.mobweb.moneyback.adapters;

import java.util.ArrayList;

import es.upv.epsa.mobweb.moneyback.R;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 *
 * @author Innovatec Sensing and Communication, S.L.
 *
 */

public class TwoLinesListItemAdapter extends BaseAdapter {
	private LayoutInflater 	inflater;
	private ArrayList<?> 	data;
	//private int[] 			colors = new int[] {0xe8e2d7, 0xf9e9c9};

	public TwoLinesListItemAdapter(Activity activity, ArrayList<?> data){
		this.inflater 		= (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.data 			= data;
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getCount()
	 */
	@Override
	public int getCount() {
		return data.size();
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getItem(int)
	 */
	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getItemId(int)
	 */
	@Override
	public long getItemId(int position) {
		return position;
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder 	= null;
		String[] values 	= (String[]) getItem(position);

		// Si la vista no está creada, se crea y se localizan los campos
		if(convertView == null){
			// Inflamos el layout
			convertView = inflater.inflate(R.layout.listitem_friend, null);

			// Localizamos los campos
			holder 				= new ViewHolder();
			holder.hTitle 		= (TextView) convertView.findViewById(R.id.txt1);
			holder.hDescription = (TextView) convertView.findViewById(R.id.txt2);

			// Asociamos el contenedor a la propiedad tag de la vista
			convertView.setTag(holder);
		}else{
			// Si la vista ya está creada, recuperamos el contenedor
			// de la propiedad tag de la lista
			holder = (ViewHolder) convertView.getTag();
		}

		holder.hTitle.setText(values[0]);
		holder.hDescription.setText(values[1]);
		

		// Colores alternos para las filas
		//int colorPos = position % colors.length;
		//convertView.setBackgroundColor(colors[colorPos]);

		// Devolvemos la vista
		return convertView;
	}

	// Clase contenedor asociada a la vista
	class ViewHolder{
		TextView hTitle;
		TextView hDescription;
	}
}
